package midi;

import java.util.Enumeration;
import java.util.Hashtable;

import midi.IntervalOperations.interval;


public class Pitch {
	private String pitch;
	private int noteNumber;
	
	
	private static Hashtable<String, int[]> pitchtable = new Hashtable<String, int[]>();
	

	public Pitch(String pitch){
		setNoteName(pitch);
	}
	
	public Pitch(int noteNumber){
		setNoteNumber(noteNumber);
	}
	
	public String getNoteName(){
		return pitch;
	}
	
	public void setNoteName(String pitch){
		this.pitch = pitch;
		this.noteNumber = stringToNoteNumber(pitch);
	}
	
	public int getNoteNumber(){
		return noteNumber;
	}
	
	public void setNoteNumber(int noteNumber){
		this.pitch = noteNumberToString(noteNumber);
		this.noteNumber = noteNumber;
	}
	
	public Pitch raise(interval interval){
		return new Pitch(IntervalOperations.raise(noteNumber, interval));
	}
	
	public static int stringToNoteNumber(String note){
		initPitchTable();
		
		int noteNumber = 0;
		
		if (note.length() == 0){
			noteNumber = pitchtable.get("C")[0];
			System.out.println(noteNumber);
		}
		
		String numbers = "";
		Boolean baseRead = false;
		for (int i = 0; i < note.length(); i++){
			char curr = note.charAt(i);
			if (curr == 'b'){
				noteNumber--;
				continue;
			}
			
			if (curr == '#'){
				noteNumber++;
				continue;
			}
			
			if (Character.isDigit(curr)){
				numbers = numbers.concat(Character.toString(curr));
				continue;
			}
			
			if (!baseRead){
				noteNumber += pitchtable.get(Character.toString(curr))[0];
				baseRead = true;
			}
		}
		
		if(numbers != ""){
			noteNumber += Integer.parseInt(numbers) * 10;
		}
		
		return noteNumber;
	}
	
	public static String noteNumberToString(int noteNumber){
		initPitchTable();
		
		boolean sharp = false;
		int octave = 1;
		
		while (noteNumber > 71){
			noteNumber -= 12;
			octave++;
		}
		
		while (noteNumber < 60){
			noteNumber += 12;
			octave --;
		}
		
		Enumeration<String> keys = pitchtable.keys();
		String result = "";
		
		while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            int[] valueOfKey = pitchtable.get(key);
            if (noteNumber == valueOfKey[0]) {
                result = key;
            }
        }
		
		if (result == ""){
			noteNumber--;
			sharp = true;
			
			keys = pitchtable.keys();
			while (keys.hasMoreElements()) {
	            String key = keys.nextElement();
	            int[] valueOfKey = pitchtable.get(key);
	            if (noteNumber == valueOfKey[0]) {
	                result = key;
	            }
	        }
		}
		
		if (octave != 1)
		{
			result = result.concat(Integer.toString(octave));
		}
		
		if (sharp){
			result = result.concat("#");
		}
		
        return result;

	}
	
	private static void initPitchTable(){
		pitchtable.put("C",new int[]{60});
		pitchtable.put("D", new int[]{62});
		pitchtable.put("E", new int[]{64});
		pitchtable.put("F", new int[]{65});
		pitchtable.put("G", new int[]{67});
		pitchtable.put("A", new int[]{69});
		pitchtable.put("B", new int[]{71});
	}
}
